﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FractionNumbersDemo
{
    public class FractionNumber:IFormattable
    {
        public int Numerator { get; private set; }
        public int Denominator { get; private set; }

        public FractionNumber()
            : this(0, 1)
        {

        }

        public FractionNumber(int num, int denom)
        {
            Numerator = num;
            Denominator = denom;
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == "N")
            {
                return string.Format("{0}/{1}", Numerator, Denominator);
            }
            else if(format == "R")
            {
                return string.Format("{0:f2}", (double)Numerator / Denominator);
            }
            else if (format == null)
            {
                return ((double)Numerator / Denominator).ToString();
            }
            throw new FormatException(string.Format("Unknown format {0}.", format));
        }
    }
}
